package com.control.servlet;

import com.dao.UserDao;
import com.model.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

public class registerServlet extends HttpServlet {

    UserDao userDao = new UserDao();

    public void init() throws ServletException {
        super.init();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        String action = (String) session.getAttribute("action");


        //这里判断注册
        if(action==null||action.length()<=0){

            int boo = userDao.register(req.getParameter("number"),req.getParameter("name"),req.getParameter("password"));
            if(boo == 0){
                String tip="注册失败";
                req.setAttribute("tip", tip);
                RequestDispatcher dispatcher = req.getRequestDispatcher("/jsp/register.jsp");
                dispatcher.forward(req, resp);
            }
            else{
                req.setAttribute("action", "fromRegister");
                req.setAttribute("name",req.getParameter("number"));
                req.setAttribute("password",req.getParameter("password"));
                RequestDispatcher dispatcher = req.getRequestDispatcher("/login.do");
                dispatcher.forward(req, resp);
            }
        }
        else if(action.equals("qq")){
            String openId = (String) session.getAttribute("openId");
            int boo = userDao.registerByQQ(req.getParameter("number"),req.getParameter("name"),req.getParameter("password"),openId);
            if(boo == 0){
                String tip="注册失败";
                req.setAttribute("tip", tip);
                RequestDispatcher dispatcher = req.getRequestDispatcher("/jsp/register.jsp");
                dispatcher.forward(req, resp);
            }
            else{
                req.setAttribute("action", "qq");
                req.setAttribute("openId",openId);
                RequestDispatcher dispatcher = req.getRequestDispatcher("/login.do");
                dispatcher.forward(req, resp);
            }
        }

    }
}
